package org.dfg.corejava.operator;

/**
 * 按位异或的用途：<br/>
 * 0^0=0,0^1=1 0异或任何数=任何数<br/>
 * 1^0=1,1^1=0 1异或任何数=任何数取反<br/>
 * 任何数异或自己=把自己置0<br/>
 */
public class Xor {
	public static void main(String[] args) {
		t3();
		int i = 3;
		int x = 0;
		x ^= i;
		System.out.println(x);
	}

	/*
	 * 1，将某一位翻转,即将某值从另一个值中去掉
	 */
	public static void t1() {
		int a = 15;// 1111
		int b = 3;// 0011
		int c = a ^ b;// 1100
		System.out.println(c);
	}

	/*
	 * 2，对换两只数的值
	 */
	public static void t2() {
		int a = 15;// 1111
		int b = 3;// 0011
		a = a ^ b;// 1100
		b = a ^ b;// 1111
		a = a ^ b;// 0011
		System.out.println(a + " " + b);
	}

	/*
	 * 3，判断两个值是否相等
	 */
	public static void t3() {
		System.out.println((3 ^ 6) == 0);
		int a = 3;
		int b = 3;
		System.out.println((a ^ b) == 0);
	}

	/*
	 * 4，一组数中随意拿掉一个，快速取其中被拿掉的数
	 */
	public static void t4() {
		int[] a = new int[] { 1, 2, 3, 4, 5 };
		int[] b = new int[] { 1, 2, 4, 5 };

		int i = 0;
		for (int x : a) {
			i ^= x;
		}
		for (int x : b) {
			i ^= x;
		}
		System.out.println(i);
	}
}
